package edu.sdcet.demo1013.controller;

import edu.sdcet.demo1013.entity.Student;
import edu.sdcet.demo1013.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    // 根据学号获取学生信息
    @GetMapping(value = "/getStudentById/{student_id}", produces = "application/json")
    public Student getStudentById(@PathVariable("student_id") String StudentId){
        System.out.println("student_id="+StudentId);
        Student student = studentService.getStudentById(StudentId);
        System.out.println("student="+student);
        return student;
    }


    // 获取所有学生信息
    @GetMapping(value = "/getAllStudents", produces = "application/json")
    public List<Student> getAllStudents(){
        List<Student> students = studentService.getAllStudents();
        return students;
    }


    @PostMapping(value = "/updateStudent", produces = "application/json")
    public Map<String, Object> updateStudent(@RequestBody Student student){
        Map<String, Object> result = new HashMap<>();
        try {
            System.out.println("更新学生信息：student="+student);
            studentService.updateStudent(student);
            result.put("success", true);
            result.put("message", "学生信息更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "学生信息更新失败：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
